package repository

import (
	"SeatSystem/internal/config"
	"SeatSystem/internal/model"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	"log"
)

var db *gorm.DB

func InitDB() {
	var err error
	db, err = gorm.Open("mysql", config.DbDsn)
	if err != nil {
		log.Fatal("repository.initDB.InitDB:", err)
	}
	autoMigrate()
}

func autoMigrate() {
	if db == nil {
		log.Fatal("repository.initDB.autoMigrate:ERR1")
	}
	db.Set("gorm:table_options", "ENGINE=InnoDB")
	db.AutoMigrate(&model.Class{}, &model.History{}, &model.Seat{}, &model.Time{}, &model.User{}, &model.FeedBack{})
}
